package com.gem.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
public class LoginController {

    //private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @GetMapping("/")
    public String showHome() {
        //获取当前登录用户
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        log.debug("当前登陆用户：" + name);
        return "home";
    }

    @GetMapping("/login")
    public String showLogin() {
        System.out.println("showlogin...");
        return "login";
    }

    @GetMapping("/admin")
    @ResponseBody
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String printAdmin(){
        return "ROLE_ADMIN";
    }

    @GetMapping("/user")
    @ResponseBody
    @PreAuthorize("hasRole('ROLE_USER')")
    public String printUser(){
        return "ROLE_USER";
    }
}
